home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1993 September / September 93.iso / Archives / Sound / Documents / MOD Info.txt < prev    next >
Text File  |  1993-10-02  |  26KB  |  642 lines

  1. There are _NO_ standard sample rate for the samples used in modules.
  2. But most often the samples are done on the rate called C-3 (which is
  3. about 16574 Hz if you have a PAL machine). Sometimes drums are sampled
  4. at A-3 (around 28 kHz), and some sounds are at ~8 kHz or anything else
  5. to save space.
  6.  
  7. The sample rate on each of the channels can be selected by a period value,
  8. which tells the hardware how many ~3.5 MHz clocks to count down before
  9. playing the next sample. If you have a 16 kHz sample you simply play
  10. it at a note that gives you 16 kHz sample rate. If you play it one octave
  11. lower, you get a 8 kHz sample rate (and a double period value).
  12.  
  13. Here are the magic formulas:
  14.  
  15.                 7093789.2
  16. SampleRate = --------------    (For a PAL machine)
  17.                Period * 2
  18.  
  19.                 7159090.5
  20. SampleRate = --------------    (For a NTSC machine)
  21.                Period * 2
  22.  
  23.  
  24. So, the most normal rate is (C-3, period 214):
  25.  
  26.   7093789.2
  27.  ----------- = 16574.27 Hz
  28.    214 * 2
  29.  
  30. (16726.8 Hz if you use the NTSC formula, but i have a PAL machine)
  31.  
  32. The Amiga has four channels with independent sample rates, so there are
  33. no such thing as a common rate for all channels, and there are no mixing
  34. going on.
  35.  
  36. For a list of period values and what notes they are supposed to correspond
  37. to, see the list of period values in the MOD format below.
  38.  
  39. Also note that finetuning are actually done by switching between 16 different
  40. period tables (not included here, but get the Amiga Protracker Playroutine).
  41.  
  42. ---
  43. Lars Hamre
  44. larsha@lise.unit.no
  45.  
  46.  
  47. [ A description of the mod format follows:  -Lars]
  48.  
  49.  
  50. Newsgroups: comp.sys.amiga.audio
  51. From: steinarm@ifi.uio.no (Steinar Midtskogen)
  52. Subject: Old MOD. format + PowerPacking
  53. Summary: Description of the MOD format and powerpacking
  54. Keywords: MOD, Powerpacker
  55. Organization: University of Oslo, Norway
  56. Date: Sun, 14 Mar 1993 23:49:00 GMT
  57.  
  58.   Since everybody seem to ask about the module format, here it comes.  I
  59. will post this information every forthnight, at least for a period (pun
  60. intended).
  61.  
  62.   I have put together most of the documentation about the module format
  63. from PT2.3, edited it heavily (all credit to the original authors, but I
  64. take responsibility for all errors) and added information about
  65. powerpacking and how the Amiga volume work.
  66.  
  67.   The reason for not using documentation following PT3.0, which is written
  68. by my group, is that there is no docmentation on the MOD format following
  69. that.  Our reason for not distributing the documentation of the MOD.
  70. format with the tracker is that we try to discourage the usage of the MOD.
  71. format; a new format is coming up soon.  So don't write a revolutionary
  72. piece of code based on the information presented herein; it will hopefully
  73. be outdated in only another month.
  74.  
  75. [ Outdated? I don't think so... -Lars]
  76.  
  77.   (The "I" in this file refer to Vishnu of Cryptoburners, the "we" refer to
  78. respectively Amiga Freelancers and Cryptoburners; it should be obvious from
  79. the context which of them)
  80.  
  81. Credits for original doc files:
  82.  
  83. Lars "ZAP" Hamre /Amiga Freelancers
  84.   The documentation to the song/module format.
  85.   The information about how to calculate BMPs from the Amiga CIA timings.
  86.  
  87. Peter "CRAYON" Hanning /Mushroom Studios/Noxious
  88.   Updates to the song/module format information. (I thought that it looked
  89.    pretty similar to the one from PT1.3, but he had put his name under,
  90.    so...)
  91.   The table of effect commands.
  92.  
  93. [ Hrm. Both files are mine. I can see no big changes. -Lars]
  94.  
  95.  
  96.   I have updated the information in the song/module format text, and added
  97. information on how the finetuning work.
  98.  
  99. ***********************************************************************
  100.  
  101. Protracker 2.3A Song/Module Format:
  102. -----------------------------------
  103.  
  104. Offset  Bytes  Description
  105. ------  -----  -----------
  106.    0     20    Songname. Remember to put trailing null bytes at the end...
  107.                When written by ProTracker this will be only uppercase;
  108.                there are only historical reasons for this. (And the
  109.                historical reason is that Karsten Obarski, who made the
  110.                first SoundTracker, was stupid.)
  111.  
  112. Information for sample 1-31:
  113.  
  114. Offset  Bytes  Description
  115. ------  -----  -----------
  116.   20     22    Samplename for sample 1. Pad with null bytes. Will only be
  117.                uppercase.  The samplenames are often used for storing
  118.                messages from the author; in particular, samplenames
  119.                starting with a '#' sign will generally be a message.  This
  120.                convention is a result of a player called IntuiTracker
  121.                displaying all samples starting with # as a message to the
  122.                person playing the module.
  123.   42      2    A WORD with samplelength for sample 1.  Stored as number of
  124.                words.  Multiply by two to get real sample length in bytes.
  125.                This is a big-endian number; for all PC programmers out
  126.                there, this means that to get your 8-bit-orginated format,
  127.                you have to swap the two bytes.
  128.   44      1    Lower four bits are the finetune value, stored as a signed
  129.                four bit number. The upper four bits are not used, and
  130.                should be set to zero.
  131.            They should also be masked out reading; you can never be
  132.            sure what some stupid program could have stored here...
  133.   45      1    Volume for sample 1. Range is $00-$40, or 0-64 decimal.
  134.   46      2    Repeat point for sample 1. Stored as number of words offset
  135.                from start of sample. Multiply by two to get offset in bytes.
  136.   48      2    Repeat Length for sample 1. Stored as number of words in
  137.                loop. Multiply by two to get replen in bytes.
  138.  
  139. Information for the next 30 samples starts here. It's just like the info for
  140. sample 1.
  141.  
  142. Offset  Bytes  Description
  143. ------  -----  -----------
  144.   50     30    Sample 2...
  145.   80     30    Sample 3...
  146.    .
  147.    .
  148.    .
  149.  890     30    Sample 30...
  150.  920     30    Sample 31...
  151.  
  152. Offset  Bytes  Description
  153. ------  -----  -----------
  154.  950      1    Songlength. Range is 1-128.
  155.  951      1    This byte is set to 127, so that old trackers will search
  156.                through all patterns when loading.
  157.                Noisetracker uses this byte for restart, ProTracker doesn't.
  158.  952    128    Song positions 0-127.  Each hold a number from 0-63 (or
  159.                0-127) that tells the tracker what pattern to play at that
  160.                position.
  161. 1080      4    The four letters "M.K." - This is something Mahoney & Kaktus
  162.                inserted when they increased the number of samples from
  163.                15 to 31. If it's not there, the module/song uses 15 samples
  164.                or the text has been removed to make the module harder to
  165.                rip. Startrekker puts "FLT4" or "FLT8" there instead.
  166.                If there are more than 64 patterns, PT2.3 will insert M!K!
  167.                here. (Hey - Noxious - why didn't you document the part here
  168.                relating to YOUR OWN PROGRAM? -Vishnu)
  169.  
  170. Offset  Bytes  Description
  171. ------  -----  -----------
  172. 1084    1024   Data for pattern 00.
  173.    .
  174.    .
  175.    .
  176. xxxx  Number of patterns stored is equal to the highest patternnumber
  177.       in the song position table (at offset 952-1079).
  178.  
  179.   Each note is stored as 4 bytes, and all four notes at each position in
  180. the pattern are stored after each other.
  181.  
  182. 00 -  chan1  chan2  chan3  chan4
  183. 01 -  chan1  chan2  chan3  chan4
  184. 02 -  chan1  chan2  chan3  chan4
  185. etc.
  186.  
  187. Info for each note:
  188.  
  189.  _____byte 1_____   byte2_    _____byte 3_____   byte4_
  190. /                \ /      \  /                \ /      \
  191. 0000          0000-00000000  0000          0000-00000000
  192.  
  193. Upper four    12 bits for    Lower four    Effect command.
  194. bits of sam-  note period.   bits of sam-
  195. ple number.                  ple number.
  196.  
  197.   To separate out the different parts of the note, something like this
  198. would be used (C to have it a bit portable; I like assembler and Pascal
  199. better myself):
  200.  
  201. int samplenum,effectcommand,effectdata,extendedcommand;
  202. char notename[];
  203. ...
  204.  
  205. void ProcessNote(byte notedata[]) {
  206.   extendedcommand=-1;
  207.   samplenum=(*notedata&0xF0)|(*(notedata+2)>>4);
  208.   switch(((*notedata<<8)|(*notedata))&0xfff) {
  209.     case 856: notename="C-1"; break;
  210.     case 808: notename="C#1"; break;
  211.     case 762: notename="D-1"; break;
  212.     case 856: notename="D#1"; break;
  213.    /* etc */
  214.     default: notename="???"; /* This should NOT occur; if it do, it is */
  215.                              /* not a ProTracker module! */
  216.    }
  217.    effectcommand=*(notedata+2)&0xF;
  218.    effectdata=*(notedata+3);
  219.    if effectcommand==0xE then /* Extended command */ {
  220.       extendedcommand=effectdata>>4;
  221.       effectdata&=0xf; /* Only one nibble data for extended command */
  222.    }
  223. }
  224.  
  225. Probably this isn't 100% valid C code, but I think you catch my drift...
  226.  
  227.  
  228.  
  229. Periodtable for Tuning 0, Normal
  230.   C-1 to B-1 : 856,808,762,720,678,640,604,570,538,508,480,453
  231.   C-2 to B-2 : 428,404,381,360,339,320,302,285,269,254,240,226
  232.   C-3 to B-3 : 214,202,190,180,170,160,151,143,135,127,120,113
  233.  
  234.   To determine what note to show, scan through the table until you find the
  235. same period as the one stored in byte 1-2.  Use the index to look up in a
  236. notenames table.
  237.  
  238.   If you have a bit of memory, it is probably smarter to use an 744 byte
  239. block to store the indexes relating to the different periods (ie:  at
  240. position 808 in the block you store 1, as 1 is the index of period 808.
  241. Then you use the block as a look up table)
  242.  
  243.   This is the data stored in a normal song.  A packed song starts with the
  244. four letters "PACK", and then comes the packed data.
  245.  
  246.   It is somewhat unclear to me what kind of packing that is referred to
  247. here.  One thing is clear though - it is NOT powerpacked or LHAed modules!
  248. I belive somebody (Probably Amiga Freelancers) was planning to install some
  249. form of direct packing into ProTracker, reserved this ID, and then it never
  250. came...  -Vishnu
  251.  
  252. [No. We never intended to install any packing here. This is an older
  253.  invention :)  Songs (modules WITHOUT the samples) can be packed by this
  254.  method, and it will put the "PACK" tag at the beginning of the file.
  255.  But since everyone saves modules with samples instead of songs, I wouldn't
  256.  care about it. The packer/depacker for PACK'ed files are in the PT source
  257.  code, available on aminet sites.  -Lars Hamre]
  258.  
  259.   In a module, all the samples are stored right after the patterndata.  To
  260. determine where a sample starts and stops, you use the sampleinfo
  261. structures in the beginning of the file (from offset 20).  Take a look at
  262. the mt_init routine in the playroutine, and you'll see just how it is done.
  263.   The data for a sample must _ALWAYS_ start with two zeros, as it is used for
  264. repeating is the sample is to be terminated.
  265.  
  266. [ Well, the playroutine will clear these two bytes anyway...  -Lars]
  267.  
  268. ***********************************************************************
  269.                                 Finetuning
  270.  
  271. Value:    0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
  272. Finetune: 0  +1  +2  +3  +4  +5  +6  +7  -8  -7  -6  -5  -4  -3  -2  -1
  273.  
  274.  
  275.   Finetuning are done by multiplying the frequency of the playback by
  276. X^(finetune), where X ~= 1.0072382087
  277.   This means that Amiga PERIODS, which represent delay times before
  278. fetching the next sample, should be multiplied by X^(-finetune)
  279.  
  280. Vishnu of Cryptoburners
  281.  
  282. [ This should be 2^(finetune/12/8). And 2^(1/12/8) is 1.007246412 on
  283.   my calculator...  (12 notes per octave and 1/8 of this)  -Lars Hamre ]
  284.  
  285.  
  286. ***********************************************************************
  287.           Decibel Values and Volume Ranges
  288.  
  289.    Volume  Decibel Value     Volume  Decibel Value
  290.  
  291.      64         0.0            32        -6.0
  292.      63        -0.1            31        -6.3
  293.      62        -0.3            30        -6.6
  294.      61        -0.4            29        -6.9
  295.      60        -0.6            28        -7.2
  296.      59        -0.7            27        -7.5
  297.      58        -0.9            26        -7.8
  298.      57        -1.0            25        -8.2
  299.      56        -1.2            24        -8.5
  300.      55        -1.3            23        -8.9
  301.      54        -1.5            22        -9.3
  302.      53        -1.6            21        -9.7
  303.      52        -1.8            20       -10.1
  304.      51        -2.0            19       -10.5
  305.      50        -2.1            18       -11.0
  306.      49        -2.3            17       -11.5
  307.      48        -2.5            16       -12.0
  308.      47        -2.7            15       -12.6
  309.      46        -2.9            14       -13.2
  310.      45        -3.1            13       -13.8
  311.      44        -3.3            12       -14.5
  312.      43        -3.5            11       -15.3
  313.      42        -3.7            10       -16.1
  314.      41        -3.9             9       -17.0
  315.      40        -4.1             8       -18.1
  316.      39        -4.3             7       -19.2
  317.      38        -4.5             6       -20.6
  318.      37        -4.8             5       -22.1
  319.      36        -5.0             4       -24.1
  320.      35        -5.2             3       -26.6
  321.      34        -5.5             2       -30.1
  322.      33        -5.8             1       -36.1
  323.                                 0    Minus infinity
  324.  
  325.   The reason for the table starting at 0 dB as the convention from
  326. taperecorders of having 0 dB as the optimal recording condition, and
  327. displaying anything worse as a negative number.
  328.  
  329.   Decibel is a logrithmical unit, just like we feel sound intensity.  It
  330. represent the ratio between two intensities.
  331.  
  332.   On the other hand, the Amiga volumes represent the linear difference
  333. between sound intensities; this mean that you have less accuracy between
  334. the low volumes than between the high ones.  If you need to, you can safely
  335. remove volume 64 and replace it with volume 63; but you can NOT remove
  336. volume 0 and replace it with volume 1.
  337.  
  338.   If you are implementing a MOD player for another sound-device, then
  339. remember to check whether it has linear or logarithmic volume control.
  340.  
  341.   The above table can be calculated from the formula dB=20*log10(Volume/64)
  342. To go the other way, from dB to Amiga volumes, do
  343. Volume=64*10^(dB/20)
  344.  
  345.   The dB here have to do with ratios of sound, not absolute sound power.
  346. This is the way it is used in recording equipment etc, and not the for
  347. measuring absolute ear-destroying capability.
  348.  
  349.   If you need to implement volume artificially, just multiply by the volume
  350. and shift right 6 times.
  351.   If you need to mix samples on-the-fly to lower the amount of voices used,
  352. your best bet is probably doing a DCT (Convert the samples to sums of
  353. cosines) on small blocks of the sample (64 bytes?) before playing, and
  354. mixing with the exact position in which you generate a sample.  This is the
  355. only way I can think of to give adequate quality, at least.
  356.  
  357. -Vishnu of Cryptoburners
  358.  
  359. ***********************************************************************
  360. Protracker V2.3A/3.01 Effect Commands
  361. ----------------------------------------------------------------------------
  362. 0 - Normal play or Arpeggio             0xy : x-first halfnote add, y-second
  363. 1 - Slide Up                            1xx : upspeed
  364. 2 - Slide Down                          2xx : downspeed
  365. 3 - Tone Portamento                     3xx : up/down speed
  366. 4 - Vibrato                             4xy : x-speed,   y-depth
  367. 5 - Tone Portamento + Volume Slide      5xy : x-upspeed, y-downspeed
  368. 6 - Vibrato + Volume Slide              6xy : x-upspeed, y-downspeed
  369. 7 - Tremolo                             7xy : x-speed,   y-depth
  370. 8 - NOT USED
  371. 9 - Set SampleOffset                    9xx : offset (23 -> 2300)
  372. A - VolumeSlide                         Axy : x-upspeed, y-downspeed
  373. B - Position Jump                       Bxx : songposition
  374. C - Set Volume                          Cxx : volume, 00-40
  375. D - Pattern Break                       Dxx : break position in next patt
  376. E - E-Commands                          Exy : see below...
  377. F - Set Speed                           Fxx : speed (00-1F) / tempo (20-FF)
  378. ----------------------------------------------------------------------------
  379. E0- Set Filter                          E0x : 0-filter on, 1-filter off
  380. E1- FineSlide Up                        E1x : value
  381. E2- FineSlide Down                      E2x : value
  382. E3- Glissando Control                   E3x : 0-off, 1-on (use with tonep.)
  383. E4- Set Vibrato Waveform                E4x : 0-sine, 1-ramp down, 2-square
  384. E5- Set Loop                            E5x : set loop point
  385. E6- Jump to Loop                        E6x : jump to loop, play x times
  386. E7- Set Tremolo Waveform                E7x : 0-sine, 1-ramp down. 2-square
  387. E8- NOT USED
  388. E9- Retrig Note                         E9x : retrig from note + x vblanks
  389. EA- Fine VolumeSlide Up                 EAx : add x to volume
  390. EB- Fine VolumeSlide Down               EBx : subtract x from volume
  391. EC- NoteCut                             ECx : cut from note + x vblanks
  392. ED- NoteDelay                           EDx : delay note x vblanks
  393. EE- PatternDelay                        EEx : delay pattern x notes
  394. EF- Invert Loop                         EFx : speed
  395. ----------------------------------------------------------------------------
  396. Peter "CRAYON" Hanning /Mushroom Studios/Noxious
  397.  
  398. For a more complete description see my previous post about the new format.
  399. This format also allow you to have more effects, and several effects on the
  400. same note.  Hopefully, it will soon replace the module format.
  401.  - Vishnu
  402.  
  403. ***********************************************************************
  404.  
  405. Protracker CIA (Complex Interface Adapter) Timer Tempo Calculations:
  406. --------------------------------------------------------------------
  407. Fcolor                        = 4.43361825 MHz (PAL color carrier frequency)
  408. CPU Clock   = Fcolor * 1.6    = 7.0937892  MHz
  409. CIA Clock   = Cpu Clock / 10  = 709.37892  kHz
  410. 50 Hz Timer = CIA Clock / 50  = 14187.5784
  411. Tempo num.  = 50 Hz Timer*125 = 1773447
  412.  
  413. For NTSC: CPU Clock = 7.1590905 MHz --> Tempo num. = 1789773
  414.  
  415.  To calculate tempo we use the formula: TimerValue = 1773447 / Tempo
  416.  The timer is only a word, so the available tempo range is 28-255 (++).
  417.  Tempo 125 will give a normal 50 Hz timer (VBlank).
  418.  
  419.  A normal Protracker VBlank song tempo can be calculated as follows:
  420.  We want to know the tempo in BPM (Beats Per Minute), or rather quarter-
  421.  notes per minute. Four notes makes up a quarternote.
  422.  First find interrupts per minute: 60 seconds * 50 per second = 3000
  423.  Divide by interrupts per quarter note = 4 notes * speed
  424.  This gives: Tempo = 3000/(4*speed)
  425.  simplified: Tempo = 750/speed
  426.  For a normal song in speed 6 this formula gives: 750/6 = 125 BPM
  427.  
  428.  Lars "ZAP" Hamre/Amiga Freelancers 1990
  429.  
  430. ***********************************************************************
  431.  
  432. The "PowerPacker" crunching algorithm:
  433.  
  434. Powerpacker use a variant of Lemel-Ziv compression.  This mean that it in
  435. some cases store strings of bytes as only an offset from the current
  436. position and a counter.  (How LZ could get a patent on this is beyond me!)
  437.  
  438. A PowerPacked file has the following format:
  439.     dc.b    'PP20'            ; Identifier
  440.     dc.l    Efficiency
  441.     ... crunched data ...
  442.     dc.l    (Length*256)+NumOfBitsToDiscard
  443.  
  444. The Efficiency is 4 bytes representing the length of offset from the
  445. current position for different runs of equal bytes.  The first three are
  446. used for runs of from 2 to 4 bytes; the last is used for all runs of 5
  447. bytes and over.
  448. The length is the length of the original, UNcrunched file.
  449. The bits to be discarded are discarded off the END of the crunched data.
  450.  
  451. All bits in the crunched data are stored in reverse order, to permit
  452. decrunching in a buffer where the crunched data are loaded at the start (An
  453. 8 byte margin between the start of the file and the decrunching position is
  454. needed, though).  This mean you have to get bits in the reverse order when
  455. decrunching, and when I refer to "Get A Bit" or Get Eight Bits" or
  456. something, that is the LAST bit or bits from the source.  Kinda obvious
  457. isn't it? ;-)
  458.  
  459. This means that also when a full set of 8 bits are read from the file,
  460. their bit order are reversed.
  461.  
  462. The varying efficiencies used by PowerPacker are as follows:
  463. Fast:      9, 9, 9, 9
  464. Medicore:  9,10,10,10
  465. Good:      9,10,11,11
  466. Very Good: 9,10,12,12
  467. Best:      9,10,12,13
  468.  
  469.  
  470.                          The Decrunching Algorithm
  471.  
  472. WritePointer is a pointer to the position in memory where decrunched bytes
  473.  are currently written.
  474.  
  475. Decrunch:
  476. REPEAT
  477.   Get A Bit (X);
  478.   IF X=0 THEN Copy bytes from source;
  479.   Copy string from already decrunched part of file;
  480.     (* Done no matter what the state of X *)
  481. UNTIL WritePointer<=Start Of Decrunchbuffer;
  482. END.
  483.  
  484.  
  485. Copy bytes from source:
  486. BEGIN
  487.   n:=0;
  488.   REPEAT
  489.     Get Two Bits (X);
  490.     n:=n+X;
  491.   UNTIL X<>3;
  492.   Copy n+1 bytes as bits from Crunched Data to WritePointer;
  493.   (* At this stage, the bytes get their order of bits reversed; and
  494.      WritePointer DECREASES *)
  495. END;
  496.  
  497. Copy string from already decrunched part of file:
  498. BEGIN
  499.   Get Two Bits(n);
  500.   OffsetLen:=Efficiency[n];
  501.   IF n<>3 THEN Get OffsetLen Bits (X)
  502.   ELSE
  503.   BEGIN
  504.     Get One Bit (X);
  505.     IF X=0 THEN Get Seven Bits (X) ELSE Get OffsetLen Bits (X);
  506.     REPEAT
  507.       Get Three Bits (Y);
  508.       n:=n+Y;
  509.     UNTIL Y<>7;
  510.   END;
  511.   Copy n+2 bytes from WritePointer+X;
  512.    (* Here it is copied reversely through memory; X is constant, while
  513.       WritePointer decreases. *)
  514. END;
  515.  
  516. If you can't read this, the original decrunchroutine follows here, in 68000
  517. assembler.  It was NOT commented before I cleared it up with macros...
  518. So complaints about the comments must go to me, too!
  519.  
  520. Registers used are (in the main decrunch routine):
  521. d0 - Counter for number of bits to fetch with the READD1 macro.
  522. d1 - Return register for the READBIT macros.
  523. d2 - Used as counter register for the copy routines.
  524. d3 - Used as offset for the oldstring copy routine.
  525. d5 - Used to store the longword currently read bits from.
  526. d7 - Used for storing the number of bits left in d5.
  527.      0 means one bit left, read new longword to d5 when d7 wraps to
  528.      negative.
  529. a0 - Pointer to current longword of source (the one in d5).
  530. a1 - Current position in the buffer to decrunch to.
  531. a2 - Start of buffer to decrunch to.  Used only for checking whether the
  532.      decrunching is through.
  533. a5 - Pointer to efficiency array.
  534.  
  535. Things to consider:
  536. Bits are shifted from d5 to d1 with code like this
  537.     lsr.l    #1,d5
  538.     addx.l    d1,d1
  539. shifting bits from the BOTTOM of d5 into the BOTTOM of d1, reversing the
  540. order of the bits as they go from d5 to d1.
  541. The predecrement mode, as in
  542.     move.b    d1,-(a1)
  543. decrement a1 BEFORE writing d1.
  544.  
  545. ***********************************************************************
  546. ;
  547. ; PowerPacker Decrunch assembler subroutine V1.1
  548. ;
  549. ; call as:
  550. ;    DecrunchBuffer (endcrun, buffer, efficiency);
  551. ;                      a0       a1        d0
  552. ; with:
  553. ;    endcrun   : UBYTE * just after last byte of crunched file
  554. ;    buffer    : UBYTE * to memory block to decrunch in
  555. ;    efficiency: Longword defining efficiency with wich file was crunched
  556. ;
  557. ; NOTE:
  558. ;    Decrunch a few bytes higher (safety margin) than the crunched file
  559. ;    to decrunch in the same memory space. (64 bytes suffice)
  560. ;
  561.  
  562. Decrunch:
  563.     lea    myBitsTable(pc),a5    ; Efficiency array
  564.     move.l    d0,(a5)            ; Store efficiency for this file.
  565.     move.l    a1,a2            ; Store start of decrunch memory.
  566.     move.l    -(a0),d5        ; Get length & number of bits to 
  567.     moveq    #0,d1            ;  trash...
  568.     move.b    d5,d1            ; Copy number of bits to trash...
  569.     lsr.l    #8,d5            ; Find length of decrunched file...
  570.     add.l    d5,a1            ; And end of decrunch buffer.
  571.     move.l    -(a0),d5        ; First longword of crunched data.
  572.     lsr.l    d1,d5            ; Skip unused bits...
  573.     moveq    #32-1,d7        ; Number of bits in longword...
  574.     sub.b    d1,d7            ; And be sure to read another when 
  575.                     ;  d5 is spent!
  576.  
  577. LoopCheckCrunch:            ; The decrunch loop.
  578.     READBIT                ; State of bit is returned in d1
  579.     bne.s    CrunchedBytes        ; and Z flag
  580. NormalBytes:
  581.     moveq    #0,d2
  582. Read2BitsRow:
  583.     READBITS #2,d1            ; Get length of run-1
  584.     add.w    d1,d2            ; Loop until not %11, increasing
  585.     cmp.w    #3,d1            ; all the time... (Sort of Huffman
  586.     beq.s    Read2BitsRow        ; on the run lengths...)
  587. .ReadNormalByte:            ; REPEAT
  588.     READBITS #8,d1            ; Get from crunched data...
  589.     move.b    d1,-(a1)        ; Store...
  590.     dbf    d2,.ReadNormalByte    ; UNTIL d2<0
  591.     cmp.l    a1,a2            ; End of crunch?
  592.     bcs.s    CrunchedBytes        ; Nope, now do crunched bytes...
  593.     rts                ; Chicken out - FINITO!
  594.  
  595. CrunchedBytes:
  596.     READBITS #2,d1            ; Get 2 bits of runlength-2
  597.     moveq    #0,d0
  598.     move.b    (a5,d1.w),d0        ; Get number of bits offset for
  599.     move.w    d1,d2            ; this runlength
  600.     addq.w    #1,d2            ; Runlength always 2+
  601.     cmp.w    #3+1,d2            ; Did data indicate longer run?
  602.     bne.s    ReadOffset        ; Nope....
  603.     READBIT                ; Is the longer run with offsetlen
  604.     bne.s    .LongBlockOffset    ; from Efficiency?
  605.     moveq    #7,d0            ; Nope, hard code length 7
  606. .LongBlockOffset:
  607.     READD1                ; Get offset...
  608.     move.w    d1,d3
  609. Read3BitsRow:
  610.     READBITS #3,d1            ; Get more string length...
  611.     add.w    d1,d2            ; Increase until stop indicated...
  612.     cmp.w    #7,d1            ; By not having the max value.
  613.     beq.s    Read3BitsRow
  614.     bra.s    DecrunchBlock        ; And start the copying.
  615. ReadOffset:
  616.     READD1                ; Get offset for short run...
  617.     move.w    d1,d3            ; and use it!
  618. DecrunchBlock:
  619.     move.b    (a1,d3.w),-(a1)        ; Loop the copy...
  620.     dbf    d2,DecrunchBlock    ; One time more than the initial d2
  621. EndOfLoop:
  622. _pp_DecrunchColor:
  623.     move.w    a1,$dff1a2        ; Set colour
  624.     cmp.l    a1,a2            ; Check if we have reached/passed 
  625.     bcs    LoopCheckCrunch        ;  lower limit...
  626.     rts                ; Yeah - chicken out!
  627.  
  628. myBitsTable:
  629.     dc.b    $09,$0a,$0b,$0b        ; Efficiency table
  630. ***********************************************************************
  631.  
  632. That's all for now - hope you don't mind...
  633. If you want to use the PowerPacker decrunching routine, I suggest instead
  634. using powerpacker.library if you are on the Amiga; if it is for a demo, I
  635. suggest using another cruncher, for instance CrunchMaster.  It has better
  636. compression ratios than PowerPacker.
  637.  
  638. Vishnu CRB           Feel free to e-mail me.
  639. steinarm@ifi.uio.no  "...all the modern inconveniences..." (Mark Twain)
  640.  
  641.  
  642.